package com.wms.service;

import com.mes.entity.User;
import com.yy.base.common.dao.Dao;
import com.yy.base.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class XtReturnService {
    @Autowired
    private Dao dao;


    //根据传过来的RFID号查询item表中是否存在RFID
    public List<Map<String,Object>> getBatchnorLists(String batch_no) {

        return dao.query("SELECT  \n" +
                "   mx.wms_storage_batch_item_id as ITEMID, pc.wms_storage_batch_id as BATCHID,  pc.c_uom_id as DWID,  \n" +
                " wl.m_product_id as WLID, wl.value as WLVALUE, wl.name as WLNAME,to_char(now(), 'yyyy-mm-dd hh24:mi:ss') as TIMES, \n" +
                " pc.emes_size_id as CMID , mx.status as STATUS \n" +
                "FROM  \n" +
                " wms_storage_batch pc,  \n" +
                " m_product wl ,  \n" +
                " wms_storage_batch_item mx  \n" +
                "WHERE  \n" +
                " pc.m_product_id = wl.m_product_id  \n" +
                " and pc.wms_storage_batch_id = mx.wms_storage_batch_id  \n" +
                " and mx.code = '"+batch_no+"'  \n" +
                " group by   wl.m_product_id ,   pc.emes_size_id  ,mx.status ,mx.wms_storage_batch_item_id,wl.name,pc.wms_storage_batch_id,  pc.c_uom_id ");
    }


    /**
     * 往inout表中插入成品信息
     * @return
     */
    @SuppressWarnings("unchecked")
    public boolean inStorageInOut(String warehouseID,String batchId, String locatorID, int qty, String uomID, String jobid, User user, String jobcode) {
        boolean result = false;
        String uuids = StringUtil.getUUID();

        String sql = "insert into wms_storage_in_out "
                + " (Wms_Storage_In_Out_ID,Wms_Storage_Batch_ID,M_Warehouse_ID,M_Locator_ID,Qty,C_Uom_ID,Opt_Mode,Opt_Code,Data_Id,Status,"
                + "AD_CLIENT_ID, AD_ORG_ID,ISACTIVE, CREATED, CREATEDBY, UPDATED, UPDATEDBY)" +
                " values(?,?,?,?,?,?,?,?,?,'OK',?,?,?,now(),?,now(),?)";


        result =  dao.executeSql(sql, uuids,batchId,warehouseID, locatorID,qty,uomID, "楦头退料入库",jobcode,jobid, user.getClientId(), user.getOrgId(), "Y", user.getId(), user.getId()) > 0;

        return result;
    }



    //修改库存中wms_storage_batch_ITEM的使用状态(在车间还是在原材料仓)
    @SuppressWarnings("unchecked")
    public boolean updateStorageBatchItemStatus(String id) {
        boolean result = false;

        String Status= "CREATE";

        String sql =  "update  wms_storage_batch_item set Status = ? where wms_storage_batch_item_id = ? ";

        result = dao.executeSql(sql,Status,id) > 0;


        return result;
    }

}
